var bough = {},
	$doc; //根据用户填写的收益人为主键保存信息
$(function() {
	$doc = $(document);
	//获取参数值
	var boughtInfo = getURLParam('boughtInfo');
	if (boughtInfo) {
		var b64 = new Base64();
		boughtInfo = b64.decode(boughtInfo);
		//格式为：测试客户2$$妻子,10000,1$$本人,400000,2
		var boughtInfos = boughtInfo.split('$$');
		$('title').html(boughtInfos[0] + '的保单');
		var tr = [],
			bi;
		for (var i = 1, l = boughtInfos.length; i < l; i++) {
			bi = boughtInfos[i].split(',');
			//tr.push('<tr iperson="' + bi[0] + '">');
			tr.push('<tr>');
			tr.push('<td>' + bi[0] + '</td>');
			tr.push('<td>' + bi[1] + '</td>');
			tr.push('<td>' + bi[2] + '/7</td>');
			tr.push('</tr>');
		}
		$('.table-list tbody').html(tr.join(''));
	} else { //地址栏中没有传递的时候，从接口中拉取
		getDataInterFace({
			inter: 'getpolicybyuserid',
			openid: getURLParam('openid'),
			userid: getURLParam('userid')
		}, function(rData) {
			analysisData(rData);
			//分析完成后加载数据到界面中
			var tr = [];
			for (var key in bough) {
				tr.push('<tr iperson="' + key + '">');
				tr.push('<td>' + key + '</td>');
				tr.push('<td>' + bough[key]['count'] + '</td>');
				tr.push('<td>' + bough[key]['itypetext'].length + '/7</td>');
				tr.push('</tr>');
			}
			$('.table-list tbody').html(tr.join(''));
		});
	}
	$('.l-btn').on('touchend click', function() {
		window.location.href = "addpolicy.html?openid=" + getURLParam('openid');
	});
	$doc.on('touchend click', 'tbody tr', function(e) {
		var tar = $(e.target);
		tar = _getTarparanet(tar, {
			key: 'tagName',
			value: 'TR'
		});
		var iperson = tar.attr('iperson');
		window.location.href = "total.html?openid=" + getURLParam('openid') + '&iperson=' + iperson;
	});
});
/**
 * 分析数据，将数据解析成表格形式
 * @param {Object} data 数据集
 */
function analysisData(data) {
	if (data && data.length > 0) {
		for (var i = 0, l = data.length; i < l; i++) {
			var o = bough[data[i]['iperson']];
			if (o) { //存在就直接添加类型
				o.count += o.count + parseInt(data[i]['iamount']);
				o.itypetext.push(data[i]['itypetext']);
				o.itypetext = unique(o.itypetext);
			} else {
				bough[data[i]['iperson']] = {
					count: parseInt(data[i]['iamount']),
					itypetext: [data[i]['itypetext']]
				}
			}
		}
	}
}
/**
 * 数组去重
 * @param {Object} arr
 */
function unique(arr) {
	var result = [],
		hash = {};
	for (var i = 0, elem;
		(elem = arr[i]) != null; i++) {
		if (!hash[elem]) {
			result.push(elem);
			hash[elem] = true;
		}
	}
	return result;
	//http://www.cnblogs.com/sosoft/
}
/**
 * 获取地址栏参数值
 * @param {Object} p
 */

function getURLParam(p) {
	var href = window.location.href,
		s = window.location.search.substr(1),
		reg = new RegExp(p + "=([^&]*)"),
		sp = '?',
		ps = s.match(reg);

	if (ps != null) { // 说明该参数存在
		return ps[1];
	} else {
		return null;
	}
}
/**
 * 获取元素父元素
 *
 * @param {Object}
 *            par 元素,$对象
 * @param {Object}
 *            filter 过滤条件
 */

function _getTarparanet(par, filter) {
	return (par[0][filter['key']] == filter['value'] || par[0][filter['key']] == 'BODY') ? par : arguments.callee(par.parent(), filter);
}

function _getTarparanetByClass(par, filter) {
	return par.hasClass(filter) ? par : arguments.callee(par.parent(), filter);
}